/**
 * Copyright 2006-2015 handu.com
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.iepacj.nio.buffer;

import java.nio.ByteBuffer;

public class ByteBufferTest {

    public static void main(String[] args) {
        // batchMove();
        compact();
    }

    private static void compact() {
        String data = "iEpacJ, we will win!";
        ByteBuffer srcBuf = ByteBuffer.allocate(1024);
        srcBuf.put(data.getBytes());
        srcBuf.flip();

        byte b = srcBuf.get();
        System.out.println("读到: " + (char) b + "; 当前位置: " + srcBuf.position() +
                "; 上限: " + srcBuf.limit() + "; 容量: " + srcBuf.capacity());
        byte b2 = srcBuf.get();

        System.out.println("读到: " + (char) b2 + "; 当前位置: " + srcBuf.position() +
                "; 上限: " + srcBuf.limit() + "; 容量: " + srcBuf.capacity());

        byte b3 = srcBuf.get();

        System.out.println("读到: " + (char) b3 + "; 当前位置: " + srcBuf.position() +
                "; 上限: " + srcBuf.limit() + "; 容量: " + srcBuf.capacity());

        // iEpacJ, we will win!
        //    acJ, we will win!
        // position 与 limit之间的数据复制到buffer的开始位置
        // 复制后 position = limit - position, limit = capacity
        // 但如果position 与limit 之间没有数据的话发，就不会进行复制
        srcBuf.compact();


        System.out.println("compact后当前位置: " + srcBuf.position() + "; 上限: " + srcBuf.limit() +
                "; 容量: " + srcBuf.capacity() + "; 剩余: " + srcBuf.remaining());



        byte[] arr = new byte[srcBuf.remaining()];
        srcBuf.get(arr);

        System.out.println("compact后的字符串: " + new String(arr));
        System.out.println("dd: " + new String(new byte[1024]));
    }

    private static void batchMove() {

        ByteBuffer srcBuf = ByteBuffer.allocate(1024);
        srcBuf.put((byte)'i').put((byte)'E').put((byte)'p').put((byte)'a').put((byte)'c');

        System.out.println("当前位置: " + srcBuf.position() + ", 上限: " + srcBuf.limit() + ", 容量" + srcBuf.capacity());
        System.out.println(srcBuf.remaining() + ", " + srcBuf.hasRemaining());

        // srcBuf.limit(srcBuf.position()).position(0);
        srcBuf.flip();
        byte[] ba = new byte[srcBuf.limit()];
        // 把srcBuf中的字节数据复制到目标ba数组中
        srcBuf.get(ba);

        for (int i = 0; i < ba.length; i++) {
            if (i == ba.length - 1) {
                System.out.println((char) ba[i]);
            } else {
                System.out.print((char) ba[i]);
            }
        }
        System.out.println("--------------------------------------");
    }


}




